package com.info.zhiduoduo.core.formbean.entrustmentproject;

import com.info.zhiduoduo.common.constants.project.ProjectType;
import com.info.zhiduoduo.common.utils.StringUtil;

import java.util.Map;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class ImportRawBean {

	@ApiModelProperty("序号")
	private String index;
	@ApiModelProperty("项目名称(必填）")
	private String name;
	@ApiModelProperty("项目内容/交付标准(必填）")
	private String content;
	@ApiModelProperty("项目金额(导入时的金额),单位元")
	private String importAmount;
	@ApiModelProperty("项目分类(必填,下拉选择）")
	private String category;
	@ApiModelProperty("发票类型(必填,下拉选择）")
	private String invoiceType;
	@ApiModelProperty("发票内容(必填）")
	private String invoiceContent;
	@ApiModelProperty("收款方姓名(必填）")
	private String payeeName;
	@ApiModelProperty("手机号")
	private String phone;
	@ApiModelProperty("账号类型(必填,下拉选择)")
	private String accountType;
	@ApiModelProperty("收款方账户(必填,支付宝登陆邮箱或银行卡号）")
	private String accountNo;
	@ApiModelProperty("收款方身份证号码(必填）")
	private String payeeIdNo;
	@ApiModelProperty("自然人绑定企业")
	private String bindcompname;
	@ApiModelProperty("委托创客工作室名称(受托方)")
	private String entrusteeName;

	public static ImportRawBean ofEntrustItem(Map<Integer, String> map) {
		return new ImportRawBean().setIndex(map.get(0))
				.setName(map.get(1))
				.setContent(map.get(2))
				.setImportAmount(map.get(3))
				.setCategory(map.get(4))
				.setInvoiceType(map.get(5))
				.setInvoiceContent(map.get(6))
				.setPayeeName(map.get(7))
				.setPhone(map.get(8))
				.setAccountType(map.get(9))
				.setAccountNo(map.get(10))
				.setPayeeIdNo(map.get(11))
				.setBindcompname(map.get(12));
	}

	public static ImportRawBean ofMakerGrouponItem(Map<Integer, String> map) {
		return new ImportRawBean().setIndex(map.get(0))
				.setName(map.get(1))
				.setContent(map.get(2))
				.setImportAmount(map.get(3))
				.setCategory(map.get(4))
				.setInvoiceType(map.get(5))
				.setInvoiceContent(map.get(6))
				.setPayeeName(map.get(7))
				.setPhone(map.get(8))
				.setAccountType(map.get(9))
				.setAccountNo(map.get(10))
				.setPayeeIdNo(map.get(11))
				.setEntrusteeName(map.get(12));
	}

	public boolean checkDataValid(int projectType) {
		if (ProjectType.isEntrustProject(projectType)) {
			if (StringUtil.isAnyEmpty(index, importAmount, payeeName, accountType, accountNo, payeeIdNo)) {
				return false;
			}
		} else if (ProjectType.isMakerGrouponProject(projectType)) {
			if (StringUtil.isAnyEmpty(index, name, content, importAmount, category, invoiceType, invoiceContent, payeeName, accountType,
					accountNo, payeeIdNo)) {
				return false;
			}
		} else {
			return false;
		}

		// 去除前后空格
		index = StringUtil.trim(index);
		name = StringUtil.trim(name);
		content = StringUtil.trim(content);
		importAmount = StringUtil.trim(importAmount.replaceAll("[^\\d\\.]", ""));
		category = StringUtil.trim(category);
		invoiceType = StringUtil.trim(invoiceType);
		invoiceContent = StringUtil.trim(invoiceContent);
		payeeName = StringUtil.trim(payeeName);
		accountType = StringUtil.trim(accountType);
		String trimedAccountNo = StringUtil.trim(accountNo);
		accountNo = StringUtil.isEmail(trimedAccountNo) ? trimedAccountNo : StringUtil.prune(accountNo);
		payeeIdNo = StringUtil.prune(payeeIdNo);
		bindcompname = StringUtil.trim(bindcompname);
		phone = StringUtil.prune(phone);
		entrusteeName = StringUtil.trim(entrusteeName);

		if (StringUtil.toDouble(index) == 0) return false;
		// 支付代付金额不能小于0.1
		if (StringUtil.toDouble(importAmount) < 0.1) return false;
		// 支付代付金额不能大于49999
		if (StringUtil.toDouble(importAmount) > 49999) return false;

		return true;
	}
}
